package org.jarraiders.musiclibrary.client.presenter;

import java.util.ArrayList;
import java.util.List;

import org.jarraiders.musiclibrary.client.AlbumsServiceAsync;
import org.jarraiders.musiclibrary.client.event.AddAlbumEvent;
import org.jarraiders.musiclibrary.client.event.EditAlbumEvent;
import org.jarraiders.musiclibrary.shared.domain.AlbumDTO;
import org.jarraiders.musiclibrary.shared.domain.ArtistDTO;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.event.shared.HandlerManager;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.HasWidgets;
import com.google.gwt.user.client.ui.Widget;

public class AlbumsPresenter implements Presenter {

	private List<AlbumDTO> albumDetails;

	public interface Display {
		HasClickHandlers getAddButton();

		HasClickHandlers getDeleteButton();

		HasClickHandlers getList();

		HasClickHandlers getSearchButton();

		String getSearchText();

		void setData(List<String> data);

		int getClickedRow(ClickEvent event);

		List<Integer> getSelectedRows();

		Widget asWidget();
	}

	private final AlbumsServiceAsync rpcService;
	private final HandlerManager eventBus;
	private final Display display;

	public AlbumsPresenter(AlbumsServiceAsync rpcService, HandlerManager eventBus, Display view) {
		this.rpcService = rpcService;
		this.eventBus = eventBus;
		this.display = view;
	}

	public void bind() {
		display.getAddButton().addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				eventBus.fireEvent(new AddAlbumEvent());
			}
		});

		display.getDeleteButton().addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				//deleteSelectedAlbums();
			}
		});

		display.getList().addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				int selectedRow = display.getClickedRow(event);

				if (selectedRow >= 0) {
					String id = albumDetails.get(selectedRow).getAlbumId();
					eventBus.fireEvent(new EditAlbumEvent(id));
				}
			}
		});
		
		display.getSearchButton().addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				searchAlbums();
			}
		});
	}

	@Override
	public void go(HasWidgets container) {
		bind();
		container.clear();
		container.add(display.asWidget());
		fetchAlbumDetails();
	}

	public void sortAlbumDetails() {

		// Yes, we could use a more optimized method of sorting, but the
		// point is to create a test case that helps illustrate the higher
		// level concepts used when creating MVP-based applications.
		//
		for (int i = 0; i < albumDetails.size(); ++i) {
			for (int j = 0; j < albumDetails.size() - 1; ++j) {
				if (albumDetails.get(j).getAlbumName().compareToIgnoreCase(albumDetails.get(j + 1).getAlbumName()) >= 0) {
					AlbumDTO tmp = albumDetails.get(j);
					albumDetails.set(j, albumDetails.get(j + 1));
					albumDetails.set(j + 1, tmp);
				}
			}
		}
	}

	public void setAlbumDetails(List<AlbumDTO> albumDetails) {
		this.albumDetails = albumDetails;
	}

	public AlbumDTO getAlbumDetail(int index) {
		return albumDetails.get(index);
	}

	private void fetchAlbumDetails() {
		rpcService.getAlbumDetails(new AsyncCallback<ArrayList<AlbumDTO>>() {
			public void onSuccess(ArrayList<AlbumDTO> result) {
				albumDetails = result;
				sortAlbumDetails();
				List<String> data = new ArrayList<String>();

				for (int i = 0; i < result.size(); ++i) {
					data.add(albumDetails.get(i).getAlbumName());
				}

				display.setData(data);
			}

			public void onFailure(Throwable caught) {
				Window.alert("Error fetching contact details");
			}
		});
	}

	/*private void deleteSelectedAlbums() {
		List<Integer> selectedRows = display.getSelectedRows();
		ArrayList<String> ids = new ArrayList<String>();

		for (int i = 0; i < selectedRows.size(); ++i) {
			ids.add(albumDetails.get(selectedRows.get(i)).getAlbumId());
		}

		rpcService.deleteAlbums(ids, new AsyncCallback<ArrayList<AlbumDTO>>() {
			public void onSuccess(ArrayList<AlbumDTO> result) {
				albumDetails = result;
				sortAlbumDetails();
				List<String> data = new ArrayList<String>();

				for (int i = 0; i < result.size(); ++i) {
					data.add(albumDetails.get(i).getAlbumName());
				}

				display.setData(data);

			}

			public void onFailure(Throwable caught) {
				Window.alert("Error deleting selected contacts");
			}
		});
	}*/
	
	private void searchAlbums(){
		rpcService.searchAlbum(display.getSearchText(), new AsyncCallback<ArrayList<AlbumDTO>>() {

			@Override
			public void onSuccess(ArrayList<AlbumDTO> result) {
				albumDetails = result;
				sortAlbumDetails();
				List<String> data = new ArrayList<String>();

				for (int i = 0; i < result.size(); ++i) {
					data.add(albumDetails.get(i).getAlbumName());
				}

				display.setData(data);

			}

			@Override
			public void onFailure(Throwable caught) {
				Window.alert("Error searching artist");

			}
		});
	}
}
